#ifndef DXInput_h__
#define DXInput_h__
#include <ddraw.h>  // directX includes
#include <dinput.h>
#include <Windows.h>

#define MOUSE_LEFT_BUTTON     0
#define MOUSE_RIGHT_BUTTON    1
#define MOUSE_MIDDLE_BUTTON   2  //(most of the time)

extern POINT  g_clientPoint;
extern RECT   g_windowRect;
class DXInput
{
protected:
	LPDIRECTINPUT8       m_lpdi;    // dxinput object
	LPDIRECTINPUTDEVICE8 m_lpdikey;    // dxinput keyboard
	LPDIRECTINPUTDEVICE8 m_lpdimouse;    // dxinput mouse

	UCHAR m_keyboardState[256]; // contains keyboard state table
	DIMOUSESTATE m_mouseState;  // contains state of mouse

	HINSTANCE m_mainInstance;
	HWND m_mainWnd;
	POINT m_mousePoint;

protected:
	int InitMouse();
	int InitKeyboard();
	int GetKeyboardData();
	int GetMouseData();
	void ReleaseMouse();
	void ReleaseKeyboard();

public:
	DXInput(HINSTANCE mainInstance, HWND mainWnd);
	int Initialize();
	void Release();
	
	//in game developing, generally, we should call this method in each turn . 
	int GetInputData();

	//these methods below shouldn't call before GetInputData();
	inline int IsKeyDown(UCHAR KEY);
	inline int IsMouseLDown();
	inline int IsMouseRDown();
	inline POINT GetCurMousePoint();
};

#endif // DXInput_h__

POINT DXInput::GetCurMousePoint()
{
	m_mousePoint.x -= g_windowRect.left;
	m_mousePoint.y -= g_windowRect.top;
	return m_mousePoint;
}

int DXInput::IsKeyDown(UCHAR KEY)
{
	return (m_keyboardState[KEY] & 0x80);
}

int DXInput::IsMouseRDown()
{
	return (m_mouseState.rgbButtons[MOUSE_RIGHT_BUTTON] & 0x80);
}

int DXInput::IsMouseLDown()
{
	return (m_mouseState.rgbButtons[MOUSE_LEFT_BUTTON] & 0x80);
}